package cn.com.zpark.servlet_context;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @PackageName: cn.com.zpark.servlet_context
 * @ClassName: ServletContextDemo01
 * @Description:
 * @author: RZS
 * @date: 2021/11/10  11:07
 */
@WebServlet("/scd1.html")
public class ServletContextDemo01 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取servletContext对象的四种方法

        // 1、通过 GenericServlet 提供的 getServletContext() 方法
        ServletContext context1 = this.getServletContext();

        // 2、通过 ServletConfig 提供的 getServletContext() 方法
        // 获取servletConfig
        ServletConfig servletConfig = this.getServletConfig();
        ServletContext context2 = servletConfig.getServletContext();

        // 3、通过 HttpSession 提供的 getServletContext() 方法
        // 获取session对象
        HttpSession session = req.getSession();
        ServletContext context3 = session.getServletContext();

        // 4、通过 HttpServletRequest 提供的 getServletContext() 方法
        ServletContext context4 = req.getServletContext();

        System.out.println(context1);
        System.out.println(context2);
        System.out.println(context3);
        System.out.println(context4);
    }
}
